using System;
using System.Collections.Generic;
using System.Text;

namespace Dogfight2008
{
  public class GameDB
  {
    Dictionary<Type, Set<object>> entities = new Dictionary<Type, Set<object>>();

    public GameDB(params Type[] types)
    {
      if (types == null || types.Length == 0)
        throw new Exception("Need at least one type");
      foreach (Type type in types)
        entities[type] = new Set<object>();
    }

    public void Add(object obj)
    {
      foreach (Type type in entities.Keys)
        if (type.IsInstanceOfType(obj))
          entities[type].Add(obj);
    }

    public void AddRange(IEnumerable<object> objs)
    {
      foreach (object obj in objs)
        Add(obj);
    }

    public void Remove(object obj)
    {
      foreach (Type type in entities.Keys)
        entities[type].Remove(obj);
    }

    public int Count(Type type)
    {
      if(!entities.ContainsKey(type))
        throw new Exception("Cannot count unknown type " + type.Name);

      return entities[type].Count;
    }

    public IEnumerable<object> Select(Type type)
    {
      return entities[type];
    }
  }
}
